package game;

import interfaces.AgentInterface;
import interfaces.impl.HumanInteraction;

import java.util.ArrayList;

import constants.Message;

import objects.WorldObject;
import extra.Position;
import universe.Universe;

public class GameCastle {
	
	private Universe universe;
	
	public GameCastle(){
		universe= new Universe(1, 1);
	}
	public void load(String name){
		universe.loadFile(name);
		
		ArrayList<WorldObject> listObjects = new ArrayList<WorldObject>();
		for(WorldObject obj : universe.getListObjects()){
			System.out.println("Estoy asignando imagenes de lo que carge");
			double x = obj.getPosition().getX();
			double y = obj.getPosition().getY();
			//int id = obj.getID();
			obj = factoryNCastle.ObjectFactory.createWorldObject(obj.getObjectReference());
			double xMid =obj.getImageObject().getImageToShow().getWidth()/2;
			double yMid =obj.getImageObject().getImageToShow().getHeight()/2;
					
			obj.getImageObject().setPosition(new Position(x-xMid,y-yMid));
			obj.setPosition(new Position(x,y));
			//obj.setID(id);
			listObjects.add(obj);
		}
		universe.setListObjects(listObjects);
		
		
		
	}
	public void update(double time) {
		
		universe.update(time);
		
		
	}
	
	
	public String getMessageUpdateWorld() {
		String res = new String();
		res=Message.UPDATE_OBJECTS_WORLD+System.lineSeparator();
		for(WorldObject w : universe.getListObjects()){
			res=res+(w.messageUpdateObject());
			res=res+System.lineSeparator();
		}
		return res;
	}
	public String getMessageAgentsFromObject(double x, double y) {
		String res = new String();
		res=Message.AGENTS_FROM_OBJECT+System.lineSeparator();
		WorldObject obj =universe.clickObject((int)x, (int)y);
		if(obj!=null){
			res=res+obj.messageAgentsFromObject();
		}
		return res;
	}
	public String getParameters(String str) {
		
		String res = Message.DONT_DO_ANYTHING+System.lineSeparator();
		
		String sd[] = str.split(";");
		int x = Integer.parseInt(sd[0]);
		int y = Integer.parseInt(sd[1]);
		String name = (sd[2]);
		
		System.out.println(str);
		
		for(HumanInteraction ag : universe.clickObject(x, y).getListHumanInteractions()){
			if(ag.getName().equals(name)){
				if(ag.getTotalParameters()==0){
					ag.setActive(true);
				}
				else{
					//I set the parameters and returned in the message
					res = Message.PARAMETERS_FOR_AGENT+System.lineSeparator();
					res=res+ag.getParameters();
				}
			break;	
			}
		}
		
		
		
		
		return res;
	}
	public void setParameters(String str) {
		String parameters [] = str.split("/");
		int id = Integer.parseInt(parameters[0]);
		String name = parameters[1];
		WorldObject obj = universe.getObject(id);
		if(obj==null){
			System.out.println("NO ENCONTRE OBJETO SERVIDORRR");
			return;
		}
		System.out.println("lo encontre");
		
		for(HumanInteraction ag : obj.getListHumanInteractions()){
			if(ag.getName().equals(name)){
				ag.setParameter(str,universe);
				System.out.println("encontee el agente que parametrizo");
			}
		}
		
	}
	

	
}
